// 用户资料控制器

import userService from '../services/userService.js'
import userValidator from '../validators/userValidator.js'
import { successResponse, errorResponse, unauthorizedResponse } from '../utils/response.js'

/**
 * 获取用户资料
 */
const getProfile = async (req, res) => {
  try {
    const userId = req.user.id
    const user = await userService.getUserProfile(userId)
    
    return successResponse(res, user, '获取用户资料成功')
  } catch (error) {
    return errorResponse(res, error.message)
  }
}

/**
 * 更新用户资料
 */
const updateProfile = async (req, res) => {
  try {
    const userId = req.user.id
    const { isValid, errors } = userValidator.validateProfileData(req.body)
    
    if (!isValid) {
      return errorResponse(res, errors.join(', '))
    }
    
    const user = await userService.updateUserProfile(userId, req.body)
    
    return successResponse(res, user, '更新用户资料成功')
  } catch (error) {
    return errorResponse(res, error.message)
  }
}

export default {
  getProfile,
  updateProfile
} 